package com.lamer.console.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lamer.console.base.entity.RoleMenuRelation;
import com.lamer.console.base.mapper.RoleMenuRelationMapper;
import com.lamer.console.base.service.IRoleMenuRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 黄诗鹏
 * @since 2021-11-11
 */
@Service
public class RoleMenuRelationServiceImpl extends ServiceImpl<RoleMenuRelationMapper, RoleMenuRelation> implements IRoleMenuRelationService {

    @Resource
    private RoleMenuRelationMapper roleMenuRelationMapper;

    @Override
    @Transactional
    public boolean allocateAuthority(int roleId, List<Integer> menuIds) {
        deleteByRoleId(roleId);//先删掉该角色下的菜单关系
        int index = roleMenuRelationMapper.insertBatch(roleId, menuIds);
        return index >= 0;
    }
    private void deleteByRoleId(int roleId){
        LambdaQueryWrapper<RoleMenuRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenuRelation::getRoleId, roleId);
        roleMenuRelationMapper.delete(queryWrapper);
    }

}
